package ai.people.platform.jx.controller;

import ai.people.netmon.framework.domain.archives.dto.ThirdPartySearchPageResponse;
import ai.people.netmon.framework.domain.jx.FbAccount;
import ai.people.netmon.framework.domain.searchmsg.request.InformationSearchPageParamVO;
import ai.people.netmon.framework.domain.searchmsg.vo.InformationSearchListRespVO;
import ai.people.netmon.framework.domain.searchmsg.vo.TgListRespFieldsVO;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;

/**
 * @author LiGuanNan
 * @date 2022/8/11 14:02
 */
@RequestMapping("/fb")
@RestController
public class FbAccountSearchController {

    @Autowired
    @Qualifier("flElasticsearchRestTemplate")
    private ElasticsearchRestTemplate flElasticsearchRestTemplate;


    @GetMapping("/search")
    public void uploadMessageExcelByAccountIdList(@RequestParam(required = false) String accountIds){
        //String[] strings = accountIds.split(",");
        //String ids = "50130979275,50149188387,50173162778,50187343260,50187751113,50195720546,50196126458,50209046818,50219242439,50219430674,50227193405,50238033588,50321662943,50333542419,50337803403,50350492816,50355983705,50367828071,50389894469,50413313084,50432250008,50446138018,50448467750,50477702237,50515229157,50550454184,50558402715,50576790299,50654869095,50654994432,50658391728,50682609888,50693138138,50698324373,50766250887,50800588240,50839917085,50882463115,50967528960,50986159575,51107963534,51126966124,51147948928,51198981868,51224483451,51348665396,51354436993,51365674596,51464102567,51493857769,51600359534,51620386454,51638951177,51642564435,51722098510,51754649644,51755732759,51790559057,51834957489,51931216313,51949034970,51982885041,51990517843,52019791573,52108629758,52119554381,52156778950,52179660826,52200775114,52206182851,52220618679,52287073906,52297049535,52301589903,52303515229,52324158392,52355371499,52358308991,52360022986,52364416314,52386432603,52396266310,52406149591,52420168901,52476786478,52496264854,52528366537,52546985981,52567995617,52576129286,52587054623,52589561551,52593926530,52602354070,52645704841,52653722043,52866415928,52924314380,53444045989,53626699093,53639895451,53654489101,53659488974,53662852206,53670217414,53705295711,53707505072,53720816770,53725377788,53738166730,53750654610,53762856758,53788620694,53789303263,53792463180,53801561805,53813338074,53815968739,53816275478,53825194723,53828871384,53862454298,53879107680,53887967183,53889371240,53902963823,53909878612,53950718052,53952869354,53954623670,53973824214,53993973660,54025481105,54030078951,54031607831,54034090381,54049242220,54058402222,54060902946,54087952841,54114372448,54115800846,54127131505,54130774667,54138680158,54164649251,54168169310,54185757622,54188987384,54199731703,54202042365,54209463809,54209850939,54211732954,54219517161,54259173057,54302134971,54326434669,54329362839,54332939765,54333224132,54346762077,54360401004,54395395764,54422376950,54472514867,545009468,54502514982,54525105621,54541494232,54655099091,54672932575,54740222845,54754988878,54799561607,55064216311,55139978107,55192469996,55210243598,55287502865,55342629084,55425321496,55431914588,55436896972,55499117230,55779104577,55791796222,55805876860,55811551319,55822093703,55843169994,55843603818,55854484124,55872610813,55884765630,55894844572,55897325078,55899620967,55906197049,55940038225,55944394623,55958133900,55964162286,55972063908,55979377851,55983019656,55984857501,55996859719,56019709185,56060279227,56074056651,56079974670,56106665943,56107702237,56124123850,56126682091,56127167550,56211234597,56244312798,56281392992,56338245325,5633999333,56363401649,56363472157,56396141107,56397164125,56401734961,56408864083,5641419878,56480644918,56489336344,56502785989,56503297156,56777943052,56821337372,56898652405,57239199360,57282854172,57287018238,57345658458,5755303613,57640046766,57744319833,57753743779,57757709914,57759776261,57769796455,57774549389,57799194864,57842579236,57850504344,57865538868,57885749156,57887514691,57896405171,57904346764,57938741959,57945264171,57952553294,57981098068,57986683270,58012118875,58020989185,58034831366,58046216836,58048032268,58051322414,58075812385,58105541943,58124305422,58135336841,58156015174,5818093900,58182648975,58187895138,58189042832,58189887534,58193730573,58211227583,58225567108,58229663522,5823419603,5825919293,58282388160,58303751024,58304153510,58304494987,58315472257,58332232860,5834184915,58343725617,58362289298,58366912783,58367027252,58400455509,58407158641,58413319536,58415992995,5841996529,58422765763,58444695215,58450998155,58482219946,58488142752,58523422276,58538841984,5856664787,58585100848,58602510212,58608140562,58608702187,58622757371,58630793855,5870469604,5872034478,58759668936,58766753052,58771668375,58782248387,58794911812,58797314982,5880817990,5885509067,5886078129,58895017663,58910134077,58957939534,58977289519,58977832657,58981424089,58982496470,58990514660,58997396071,5899902534,5900766725,59020719873,59037353860,59048126646,59065328242,5907031877,59071059059,5910174138,59134047924,59134852657,5913504830,59164172115,5922019510,59234383388,59242722420,59251809602,59258630679,59261833855,59272578195,59306617060,59319540080,59323386296,59329476938,59335603303,59335895677,59347026292,59347482220,59369724757,59376282855,59402805280,59406407430,59427161014,59454924161,59495026211,59499041345,59537485119,59580882121,59589668552,59618669707,59634714875,59638578832,59644808610,59686812520,59687917038,59689330275,59693297244,59697028947,59727184515,59747593370,59757028764,59770650405,59776915683,59796074158,59821427402,59823004078,59826489083,59827178017,59832554381,59848658186,59854957906,59921823097,59930478000,59930657929,59931395805,59967681374,59968372365,59972525437,59989389704,5NEWS,60058631460,60063086539,60096859041,60104274034,60107348663,60115872937,60157107973,60161659901,60166782640,60179536068,60183334754,60189066981,60223202880,60301101250,60423057547,60486733334,60509448512,60514532749,60542384195,60587934776,60628371150,60655473278,60663517288,60673679969,60680223569,60693256886,60698496021,60708531146,60709710167,60753907804,60762913584,60844985340,60881876815,60900536848,61227552052,61267906110,61287587308,61301962345,61328884890,61363496737,61378020148,61388081925,61391610659,61392066841,61406616217,61407112242,61426542790,61433589272,61437417787,61453806083,61479821781,61494232564,61495212716,61561355863,61564558040,61575637587,61587568442,61596555824,61622869019,61641075821,61660284605,61662826968,61667511637,61705726831,61713230678,61717761703,61744236822,61765180802,61771971138,61775638026,61790272417,61799588740,61800131636,61812291127,61823047118,61837310349,61846747274,61863318139,61865376860,61870914731,61871416918,61881851423,61884672898,61923873704,61941627160,61955137600,61956872921,61977911353,61978668141,61999622226,62023146758,62082505853,62232870730,62361066261,62364452460,62367928505,62436004642,62569524052,62651751345,62710518060,62794163141,62869253227,63022278271,63088415063,63177185688,63240972480,63311616719,63398470842,63464009352,63493540975,63615667798,63642168281,63661435443,63690514258,63806824514,63856770604,63948584192,63967947818,64086616261,64139256130,64234913544,64240976480,64381916155,64442722002,64728262243,64735471245,64760528543,64768707927,64779275389,64784569613,64787602536,64788267513,64802312314,64817121270,64817746785,64882095705,64895975881,64896328812,64899024011,64903608617,64943530753,64945928037,64958729656,64965843128,64968240897,65023211067,65029670232,65033531276,65048499004,65069115489,65069995604,65091817276,65133747933,65249967120,65254447721,65255771213,65268895848,65273767128,65292696334,65344286949,65348550855,65361260032,65392946338,65396050344,65422126433,65458590874,65465585125,65476169630,65529103190,65532806968,65599049452,65710511268,65737832194,65752721480,65819136057,65880247179,66060561704,66072336689,66191235143,66260433899,66454132273,66517882315,66821423238,66837349347,66845185844,66874361378,66962184341,66963842167,67003546475,67053282137,67054826509,67262126186,67303166505,67313876473,67380435328,67382306960,67426331609,67451472575,67452172207,67469076792,67532237627,67649894645,67686808955,67688043579,67773657044,67884984384,68030272440,68090970431,68107234702,68194304287,68302436412,68312339694,68329275311,68361158364,68373246741,68387696720,68411012232,68417080436,68420833372,68428499712,68441743439,68452922578,68474295749,68496394127,68516873295,68517413622,68551557547,68621004939,68634525159,68680511262,68701993173,68714578469,68729729476,68735901884,68737772478,68741863973,68793499001,68870864697,68918564822,68921437212,68934618495,68999954133,69013050676,69224215560,69577522666,69734604298,70248533505,70312675632,70405444202,70423624473,70449678440,70464625800,70525678510,70541264720,70542523141,70559617573,70598376124,70620174354,70623302410,70683959198,70733203874,70741217786,70756052324,70774840300,70801478123,70827155740,70847349631,70923817977,70931639997,70937137362,70940717941,70965219188,71038290898,71041204993,71044421803,71091031475,71111379908,71114335403,71129798966,71144068956,71160696039,71192262880,71211585334,71257806402,71263708835,71284685294,71344833955,713463241,71381411432,71431179314,71436929476,71445558070,71457224102,71558806767,71618421459,71628823287,71629522981,71668328440,71689558267,71691515445,71708644663,71725632562,71741701887,71751100093,71759056251,71768358247,71790482930,71792221341,71824856211,71866099591,71867775025,71871584524,71888146641,71937012721,71942358221,71969443222,71977064964,71984177004,72005005409,72015164219,72039016801,72050055207,72077171662,72082045465,72082456013,72088371339,72099108775,72134269075,72141379006,72210698346,72219280428,72220983233,72224899516,72238497792,72249031214,72276163077,72287163741,72294968314,72333053744,72350663020,72359848038,72430188661,72444082118,72450458708,72454684824,72486000913,72488387473,72495479655,72510602621,72546266913,72552767965,72602371663,72604591412,73344030434,73400644755,73473921694,74140907087,74560667973,74567188497,74576662645,74589197827,74631360824,74636688310,74676188596,74695469931,74727918361,74765538465,74765722914,74782118702,74801323026,74835105208,74844192144,74885309813,74934086143,74952105957,74952596374,74964244550,74989142602,75027787102,75717012509,75727095733,75730762263,75755875686,75761327340,75761984093,75859026585,75860411475,75953863801,75989049067,76054692957,76070643395,76129319111,76165887029,76204128491,76230208196,76237796088,76245308468,76350804017,76363260981,76380262397,76416078870,76528042101,76588423378,76610284508,76639961047,76751547051,76834420337,76862956139,76952916976,76988022226,76997257565,77151396330,77192606438,77285548402,77324622771,77425307448,77467208393,77511664762,77605238952,77711773310,77743838150,77789850841,77820033168,77883172773,77900948669,77951434370,78083501234,78118717073,78180181975,78251417859,78265719721,78334215302,78375212122,78469976452,78500745078,78507498093,78538877233,78565254903,78677647169,78695947978,78709192499,78729709465,78735572813,78765764954,78807369108,78816094568,78827521957,78831460519,78917621860,78929433679,78943952916,78976852029,79073670801,79101426653,79109641477,79150362181,79167888410,79170554558,79173856440,79186409788,79192737851,79204354989,79213385913,79227659117,79243109393,79245721955,79259439409,79280332867,79311193417,79325958739,79332913550,79355523842,79368985740,79382937488,79401643939,79407731407,79439269326,79455291594,79464761032,79485722945,79500937678,79511407410,79516860126,79558050564,79581059050,79593391674,79602517623,79602524701,79645928247,79654079246,79662105955,79674996532,79677029651,79700925654,79706755508,79751316300,79765636879,79777114645,79787342129,79820371749,79863788373,79886333749,79892498171,79940397012,79944443582,79946469932,79985406197,79998253416,80004928636,80006225690,80012040801,80034959558,80318679269,80658565761,80824331898,80856241270,80974751656,81147439122,81305481026,81505865303,82017961605,82318838684,82653488401,82745917675,82751110597,82789872735,82796200788,82826952813,82831366106,82847078492,82865721195,82888841135,82926846143,82936675100,82941113882,82992287444,83038008044,83063942708,83101441077,83105098457,83138262041,83156689739,83174831783,83179811145,83186394667,83198947990,83226704916,83237110185,83237582338,83238889930,83272070232,83288822289,83293661255,83319173194,83364864564,83365377772,83376258404,83381777269,83397929635,83482991924,83489042018,83493202987,83539196973,83552172515,83559248493,83599524541,83606903869,83614589030,83620108528,83634167333,83647963293,83665787936,83666691552,83674707715,83678653710,83690761841,83735130796,83763138109,83793708850,83811036837,83829282573,83851482591,83870472487,83872444999,83886249302,83895163460,83941310716,83982678285,84004609541,84025489142,84030323460,84036824667,84079259891,84107366121,84137379665,84147127770,84154833263";
        String ids = "100000801816282,50130979275,50149188387,50173162778,50187343260,50187751113";
        String[] strings = ids.split(",");
        LongAdder longAdder = new LongAdder();
        List<FbAccount> list = Lists.newArrayList();
        Arrays.asList(strings).forEach(o->{
            InformationSearchPageParamVO vo = new InformationSearchPageParamVO();
            vo.setKeyword(o);
            vo.setPageNo(0);
            vo.setPageSize(10000);
            ThirdPartySearchPageResponse<FbAccount> response = queryFbInfoPage(vo);
            if(response.getTotal() > 0){
                list.add((FbAccount)response.getList().toArray()[0]);
                List<FbAccount> list1 = (List<FbAccount>) response.getList();
                if(!StringUtils.isBlank(list1.get(0).getPhone())){
                    longAdder.increment();
                }
            }
        });


        String fileName = new StringBuilder("C:\\Users\\ligua\\Desktop\\test").append("/").append("facebook").append(".xlsx").toString();
        File file = new File(fileName);
        if(file.exists()){
            file.delete();
        }
        try (ExcelWriter excelWriter = EasyExcel.write(fileName, FbAccount.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
            excelWriter.write(list, writeSheet);
        }
        System.out.println("账号命中率： " + (double)(Math.round(list.size()*100 / strings.length)/100.0));
        System.out.println("手机号命中率： " + (double)(Math.round(longAdder.longValue()*100 / strings.length)/100.0));
    }

    /**
     * 查询tg帐户分页
     *
     * @return {@link InformationSearchListRespVO}<{@link TgListRespFieldsVO}>
     */
    public ThirdPartySearchPageResponse<FbAccount> queryFbInfoPage(InformationSearchPageParamVO paramVO) {
        List<FbAccount> list = Lists.newArrayList();
        long total=0, totalPage=1;

        if(paramVO == null){
            return ThirdPartySearchPageResponse.builderEmpty();
        }

        // 创建查询对象
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        if(StringUtils.isNotBlank(paramVO.getKeyword())){
            builder.withQuery(new TermQueryBuilder( "uid",paramVO.getKeyword()));
            //builder.withQuery(new MatchQueryBuilder("uid", paramVO.getKeyword()).operator(Operator.OR));

        }
        builder.withPageable(PageRequest.of(paramVO.getPageNo(), paramVO.getPageSize()));
        NativeSearchQuery query = builder.build();

        //指定查询索引
        IndexCoordinates indexCoordinates = IndexCoordinates.of("socialmedia-facebook");

        // 调用查询接口
        SearchHits<FbAccount> searchHits = null;
        try {
            searchHits = flElasticsearchRestTemplate.search(query, FbAccount.class, indexCoordinates);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(searchHits != null){
            // 记录总数
            total = searchHits.getTotalHits();


            List<SearchHit<FbAccount>> hits = searchHits.getSearchHits();
            if(!CollectionUtils.isEmpty(hits)){
                list  = hits.stream().map(SearchHit::getContent).collect(Collectors.toList());
            }
            return ThirdPartySearchPageResponse.builder(list, total, totalPage, paramVO.getPageNo());
        }
        return ThirdPartySearchPageResponse.builderEmpty();
    }

    @GetMapping("/search2")
    public void uploadMessageExcelByAccountIdList2(@RequestParam(required = false) String accountIds){
        //String[] strings = accountIds.split(",");
        //String ids = "50130979275,50149188387,50173162778,50187343260,50187751113,50195720546,50196126458,50209046818,50219242439,50219430674,50227193405,50238033588,50321662943,50333542419,50337803403,50350492816,50355983705,50367828071,50389894469,50413313084,50432250008,50446138018,50448467750,50477702237,50515229157,50550454184,50558402715,50576790299,50654869095,50654994432,50658391728,50682609888,50693138138,50698324373,50766250887,50800588240,50839917085,50882463115,50967528960,50986159575,51107963534,51126966124,51147948928,51198981868,51224483451,51348665396,51354436993,51365674596,51464102567,51493857769,51600359534,51620386454,51638951177,51642564435,51722098510,51754649644,51755732759,51790559057,51834957489,51931216313,51949034970,51982885041,51990517843,52019791573,52108629758,52119554381,52156778950,52179660826,52200775114,52206182851,52220618679,52287073906,52297049535,52301589903,52303515229,52324158392,52355371499,52358308991,52360022986,52364416314,52386432603,52396266310,52406149591,52420168901,52476786478,52496264854,52528366537,52546985981,52567995617,52576129286,52587054623,52589561551,52593926530,52602354070,52645704841,52653722043,52866415928,52924314380,53444045989,53626699093,53639895451,53654489101,53659488974,53662852206,53670217414,53705295711,53707505072,53720816770,53725377788,53738166730,53750654610,53762856758,53788620694,53789303263,53792463180,53801561805,53813338074,53815968739,53816275478,53825194723,53828871384,53862454298,53879107680,53887967183,53889371240,53902963823,53909878612,53950718052,53952869354,53954623670,53973824214,53993973660,54025481105,54030078951,54031607831,54034090381,54049242220,54058402222,54060902946,54087952841,54114372448,54115800846,54127131505,54130774667,54138680158,54164649251,54168169310,54185757622,54188987384,54199731703,54202042365,54209463809,54209850939,54211732954,54219517161,54259173057,54302134971,54326434669,54329362839,54332939765,54333224132,54346762077,54360401004,54395395764,54422376950,54472514867,545009468,54502514982,54525105621,54541494232,54655099091,54672932575,54740222845,54754988878,54799561607,55064216311,55139978107,55192469996,55210243598,55287502865,55342629084,55425321496,55431914588,55436896972,55499117230,55779104577,55791796222,55805876860,55811551319,55822093703,55843169994,55843603818,55854484124,55872610813,55884765630,55894844572,55897325078,55899620967,55906197049,55940038225,55944394623,55958133900,55964162286,55972063908,55979377851,55983019656,55984857501,55996859719,56019709185,56060279227,56074056651,56079974670,56106665943,56107702237,56124123850,56126682091,56127167550,56211234597,56244312798,56281392992,56338245325,5633999333,56363401649,56363472157,56396141107,56397164125,56401734961,56408864083,5641419878,56480644918,56489336344,56502785989,56503297156,56777943052,56821337372,56898652405,57239199360,57282854172,57287018238,57345658458,5755303613,57640046766,57744319833,57753743779,57757709914,57759776261,57769796455,57774549389,57799194864,57842579236,57850504344,57865538868,57885749156,57887514691,57896405171,57904346764,57938741959,57945264171,57952553294,57981098068,57986683270,58012118875,58020989185,58034831366,58046216836,58048032268,58051322414,58075812385,58105541943,58124305422,58135336841,58156015174,5818093900,58182648975,58187895138,58189042832,58189887534,58193730573,58211227583,58225567108,58229663522,5823419603,5825919293,58282388160,58303751024,58304153510,58304494987,58315472257,58332232860,5834184915,58343725617,58362289298,58366912783,58367027252,58400455509,58407158641,58413319536,58415992995,5841996529,58422765763,58444695215,58450998155,58482219946,58488142752,58523422276,58538841984,5856664787,58585100848,58602510212,58608140562,58608702187,58622757371,58630793855,5870469604,5872034478,58759668936,58766753052,58771668375,58782248387,58794911812,58797314982,5880817990,5885509067,5886078129,58895017663,58910134077,58957939534,58977289519,58977832657,58981424089,58982496470,58990514660,58997396071,5899902534,5900766725,59020719873,59037353860,59048126646,59065328242,5907031877,59071059059,5910174138,59134047924,59134852657,5913504830,59164172115,5922019510,59234383388,59242722420,59251809602,59258630679,59261833855,59272578195,59306617060,59319540080,59323386296,59329476938,59335603303,59335895677,59347026292,59347482220,59369724757,59376282855,59402805280,59406407430,59427161014,59454924161,59495026211,59499041345,59537485119,59580882121,59589668552,59618669707,59634714875,59638578832,59644808610,59686812520,59687917038,59689330275,59693297244,59697028947,59727184515,59747593370,59757028764,59770650405,59776915683,59796074158,59821427402,59823004078,59826489083,59827178017,59832554381,59848658186,59854957906,59921823097,59930478000,59930657929,59931395805,59967681374,59968372365,59972525437,59989389704,5NEWS,60058631460,60063086539,60096859041,60104274034,60107348663,60115872937,60157107973,60161659901,60166782640,60179536068,60183334754,60189066981,60223202880,60301101250,60423057547,60486733334,60509448512,60514532749,60542384195,60587934776,60628371150,60655473278,60663517288,60673679969,60680223569,60693256886,60698496021,60708531146,60709710167,60753907804,60762913584,60844985340,60881876815,60900536848,61227552052,61267906110,61287587308,61301962345,61328884890,61363496737,61378020148,61388081925,61391610659,61392066841,61406616217,61407112242,61426542790,61433589272,61437417787,61453806083,61479821781,61494232564,61495212716,61561355863,61564558040,61575637587,61587568442,61596555824,61622869019,61641075821,61660284605,61662826968,61667511637,61705726831,61713230678,61717761703,61744236822,61765180802,61771971138,61775638026,61790272417,61799588740,61800131636,61812291127,61823047118,61837310349,61846747274,61863318139,61865376860,61870914731,61871416918,61881851423,61884672898,61923873704,61941627160,61955137600,61956872921,61977911353,61978668141,61999622226,62023146758,62082505853,62232870730,62361066261,62364452460,62367928505,62436004642,62569524052,62651751345,62710518060,62794163141,62869253227,63022278271,63088415063,63177185688,63240972480,63311616719,63398470842,63464009352,63493540975,63615667798,63642168281,63661435443,63690514258,63806824514,63856770604,63948584192,63967947818,64086616261,64139256130,64234913544,64240976480,64381916155,64442722002,64728262243,64735471245,64760528543,64768707927,64779275389,64784569613,64787602536,64788267513,64802312314,64817121270,64817746785,64882095705,64895975881,64896328812,64899024011,64903608617,64943530753,64945928037,64958729656,64965843128,64968240897,65023211067,65029670232,65033531276,65048499004,65069115489,65069995604,65091817276,65133747933,65249967120,65254447721,65255771213,65268895848,65273767128,65292696334,65344286949,65348550855,65361260032,65392946338,65396050344,65422126433,65458590874,65465585125,65476169630,65529103190,65532806968,65599049452,65710511268,65737832194,65752721480,65819136057,65880247179,66060561704,66072336689,66191235143,66260433899,66454132273,66517882315,66821423238,66837349347,66845185844,66874361378,66962184341,66963842167,67003546475,67053282137,67054826509,67262126186,67303166505,67313876473,67380435328,67382306960,67426331609,67451472575,67452172207,67469076792,67532237627,67649894645,67686808955,67688043579,67773657044,67884984384,68030272440,68090970431,68107234702,68194304287,68302436412,68312339694,68329275311,68361158364,68373246741,68387696720,68411012232,68417080436,68420833372,68428499712,68441743439,68452922578,68474295749,68496394127,68516873295,68517413622,68551557547,68621004939,68634525159,68680511262,68701993173,68714578469,68729729476,68735901884,68737772478,68741863973,68793499001,68870864697,68918564822,68921437212,68934618495,68999954133,69013050676,69224215560,69577522666,69734604298,70248533505,70312675632,70405444202,70423624473,70449678440,70464625800,70525678510,70541264720,70542523141,70559617573,70598376124,70620174354,70623302410,70683959198,70733203874,70741217786,70756052324,70774840300,70801478123,70827155740,70847349631,70923817977,70931639997,70937137362,70940717941,70965219188,71038290898,71041204993,71044421803,71091031475,71111379908,71114335403,71129798966,71144068956,71160696039,71192262880,71211585334,71257806402,71263708835,71284685294,71344833955,713463241,71381411432,71431179314,71436929476,71445558070,71457224102,71558806767,71618421459,71628823287,71629522981,71668328440,71689558267,71691515445,71708644663,71725632562,71741701887,71751100093,71759056251,71768358247,71790482930,71792221341,71824856211,71866099591,71867775025,71871584524,71888146641,71937012721,71942358221,71969443222,71977064964,71984177004,72005005409,72015164219,72039016801,72050055207,72077171662,72082045465,72082456013,72088371339,72099108775,72134269075,72141379006,72210698346,72219280428,72220983233,72224899516,72238497792,72249031214,72276163077,72287163741,72294968314,72333053744,72350663020,72359848038,72430188661,72444082118,72450458708,72454684824,72486000913,72488387473,72495479655,72510602621,72546266913,72552767965,72602371663,72604591412,73344030434,73400644755,73473921694,74140907087,74560667973,74567188497,74576662645,74589197827,74631360824,74636688310,74676188596,74695469931,74727918361,74765538465,74765722914,74782118702,74801323026,74835105208,74844192144,74885309813,74934086143,74952105957,74952596374,74964244550,74989142602,75027787102,75717012509,75727095733,75730762263,75755875686,75761327340,75761984093,75859026585,75860411475,75953863801,75989049067,76054692957,76070643395,76129319111,76165887029,76204128491,76230208196,76237796088,76245308468,76350804017,76363260981,76380262397,76416078870,76528042101,76588423378,76610284508,76639961047,76751547051,76834420337,76862956139,76952916976,76988022226,76997257565,77151396330,77192606438,77285548402,77324622771,77425307448,77467208393,77511664762,77605238952,77711773310,77743838150,77789850841,77820033168,77883172773,77900948669,77951434370,78083501234,78118717073,78180181975,78251417859,78265719721,78334215302,78375212122,78469976452,78500745078,78507498093,78538877233,78565254903,78677647169,78695947978,78709192499,78729709465,78735572813,78765764954,78807369108,78816094568,78827521957,78831460519,78917621860,78929433679,78943952916,78976852029,79073670801,79101426653,79109641477,79150362181,79167888410,79170554558,79173856440,79186409788,79192737851,79204354989,79213385913,79227659117,79243109393,79245721955,79259439409,79280332867,79311193417,79325958739,79332913550,79355523842,79368985740,79382937488,79401643939,79407731407,79439269326,79455291594,79464761032,79485722945,79500937678,79511407410,79516860126,79558050564,79581059050,79593391674,79602517623,79602524701,79645928247,79654079246,79662105955,79674996532,79677029651,79700925654,79706755508,79751316300,79765636879,79777114645,79787342129,79820371749,79863788373,79886333749,79892498171,79940397012,79944443582,79946469932,79985406197,79998253416,80004928636,80006225690,80012040801,80034959558,80318679269,80658565761,80824331898,80856241270,80974751656,81147439122,81305481026,81505865303,82017961605,82318838684,82653488401,82745917675,82751110597,82789872735,82796200788,82826952813,82831366106,82847078492,82865721195,82888841135,82926846143,82936675100,82941113882,82992287444,83038008044,83063942708,83101441077,83105098457,83138262041,83156689739,83174831783,83179811145,83186394667,83198947990,83226704916,83237110185,83237582338,83238889930,83272070232,83288822289,83293661255,83319173194,83364864564,83365377772,83376258404,83381777269,83397929635,83482991924,83489042018,83493202987,83539196973,83552172515,83559248493,83599524541,83606903869,83614589030,83620108528,83634167333,83647963293,83665787936,83666691552,83674707715,83678653710,83690761841,83735130796,83763138109,83793708850,83811036837,83829282573,83851482591,83870472487,83872444999,83886249302,83895163460,83941310716,83982678285,84004609541,84025489142,84030323460,84036824667,84079259891,84107366121,84137379665,84147127770,84154833263";
        //String ids = "100000801816282,50130979275,50149188387,50173162778,50187343260,50187751113";
        String ids = "100000171357222,100000257628456,100000335686769,100000342560944,100000458441864,100000679524865,100001310493689,100001801846533,100010008886901,100014173159155,100015497260105,100022357210594,100027540699057,100034668263348,100036333140,100037517678007,100045045016115,100045937969,100047496108226,100051216049654,100052268273085,100052857009202,100057477877888,100058981425807,100063003712793,100075589367737,100076622113866,100082134150382,100089313359,100096482964,100098206606,100129319682,100132500895,100141625685,100148539295,100150961991,100153779765,100158800447,100163589126,100174134425,100180858365,100189435421,100209900818,100227323987,100283087956,100315221738,100341000169,100341729358,100355428628,100394503949,100401597923,100402391319,100407799690,100409722659,100427813034,100429659640,100455969929,100458406619,100473153779,10049970637,100523402133,100526673914,100528444310,100562024445,100580254583,100582215942,100586951032,100602557977,100622063795,100642881194,100661169673,100678206005,100685156787,100729538742285,100755013798,100768333039,100785137478,100788987735,100792561847,100801244685,100807979122,100818332595,100829062304,100844313565,100857415950,100891866184,10089846244,100904403023,10091139214,100926718421,100951582887,100962549430,100977402434,100988306915,101016052597,101034177178,101043629948,101044522833,101046420317,101052973758,101064631942,101065146423,10109514234,10111396473,10111984694,101126031910,101126247062,101132357017,101140590778,101148858389,101183452743,101191063709,101205018445,101220519492,101243793801,1012566669,101266097322,101274695752,101278239698,101294832521,101296194220,101327284825,101356070565,101365094372,101388459221,10138906411,101396017884,101402598109,101403276673,101472371716,101472444002,101513415483,101551362487,101660306283,10171142198,10171432268,101727165027,101730986904,10175315501,101755297359,101762888238,10182088156,101839056732,101847417414,101891459717,10192307906,101930585125,101931294006,101940983661,101978231823,101982830708,101984379692,101993342681,102004980389,102026161027,102030281967,102094197503,102124743035,102132134160,102157437808,102175189751,102175734033,102180330512,10219227876,102200009023,102242225579,102243110324,102256749720,102259120035,102281256057,10233006053,102346196465,102375391005,10238491978,102413390866,10241966357,10247082314,10247823823,102488682345,102491342652,102493739857,102508247817,10250877124,102515127108,102527030797,10254284993,102545914378,102547347983,102591797822,102601282167,102604965216,10262075277,10262466446,102629288045,102639891554,102644633753,102647276452,102647896058,102655601414,102660618396,102745575591,10274793956,102751845800,102760330030,102781846221,102800004322,102850816117,102854478206,102858911411,102892417916,102914739740,102944597069,102954931645,102957838625,102958856450,103030619976,103037936123,10306433076,103066337429,103131622069,10313857381,103155091022,10318345895,103205049435,103228349089,103321598965,103328396827,103337108248,103381659381,103391238026,10339334796,10340165675,103427090898,103434259202,103480618943,103590278683,103601177441,103621049307,103644085535,103684678318,103688735342,103738155478,10375609111,103833576340,10384115418,103903533078,103908979661,103914456830,103941917378,10394394541,103953798253,103954840749,103966728583,104064439133,10407233798,104104799100,104115534600,104115791812,104129837848,104152941364,104170307689,104208434138,104214212153,104220701723,104330471816,104342950498,104389241433,104395909793,104399084666,104422666613,104494786019,104552658376,104565260284,10464621598,104649000318,104709558232,104711144202,104761768893,104782512165,104810394211,104830362120,104844231644,104867526497,104911865817,104955041602,104967698520,104991874784,105012307989,105013001123,105014462720,105025373720,105037513988,105089514197,105150003998,105164280037,105219292949,105227607396,10524033745,105267491339,105291049340,105308188973,105321224444,105331708115,105340035060,105374120179,105409330637,105438778195,105442976683,105450667735,105451056954,105477297718,105479542403,105488797790,10551286719,105517788913,105528706538,105550901993,105583497196,105610722392,105619022667,10562018453,105675837257,105678861504,105683110373,105686751449,105697443751,105697815298,10569996239,10570340282,105708941196,105712595448,105736898213,105765448167,105780842639,105782301620,105819078111,105823833098,105829644942,105842046268,105848992847,105858495622,105859258711,105873962132,105926872467,105951327210,10595951588,105963851294,105970161516,10607030196,106109666630,106110162177,106129088194,106129751880,106135277948,106145823184,106159049327,10616564826,106168826931,106187969025,106198396898,106198765427,106221737834,106251878963,106263555398,106268461471,106299158075,106304185015,106322753259,106324770685,106357455372,106358596596,106359333405,106386772080,106387503918,10641762262,106432518833,106444024956,10650599468,106514690178,106519026494,106531918634,106609890744,106614361145,106615770543,106616520753,106622708780,106671939087,106703786796,106739030305,106740034577,106742371628,106747329469,10676984257,10678066154,106785233543,106795721624,106824832045,106825869514,106842498811,106856083870,106865987959,106890638722,106893798196,106898437694,106903146673,10692262171,106957584526,106959080131,106967859657,10702978668,107066324640,107066923227,107068046938,107093381224,107118720945,107138149484,107190487462,107218492374,107237657667,1072578023,107281142423,107297026677,107345860546,107347859581,107350289372,107364861995,107421703315,10745509014,107462024045,107483965574,107484458589,107499474013,10750083165,107611238286,107630045473,10763717283,107639640124,107688668433,107703181781,107716748927,107745361517,107786708677,107787018440,107808239700,10782091662,107824111559,107838799590,107851729949,107886154601,107921076481,107937049551,107940162265,107951784745,107994595938,108004609888,108005463024,108033965448,108040781038,108059810718,108062134928,108071664070,108074613369,108104785149,108125997361,10812953218,108149813789,10817698614,108187353677,10821072338,108238760044,108243736654,108279113094,108308607496,108319964013,108320039416,108354499582,108388107767,10840090722,108462786961,108472866587,108480362369,108525036810,108534271332,108559765783,108568603632,108571517542,108581541186,108593048942,108600452311,108618343856,108647151155,108676558128,108694539041,108713167592,108740383769,108763692386,108772683119,108827338116,10883078204,108882546698,108886131355,10889257620,108901497117,108918241923,108926481610,108926641831,108952062428,108953626689,108953983096,109016449803,109075776460,10907772315,10908188913,10908932959,109091607082,109107141165,109107976063,109108389731,109118669024,109123822244,109133490168,109150997719,10916985377,109196331784,109210200864,10921745821,109224210772,109225967651,109261007964,109300768899,109310343632,109329384547,109345876510,109357787934,109362579338,109368874773,109383989362,109385164408,109385691839,109440138783,109440426760,109449408709,109496746757,109509112539,109517149718,109526169660,109538411270,109571966014,109576152178,109625394181,109625921527,109651244538,109671977361,109676300884,109712071254,109735866254,109737394571,10974002364,109752866248,10976724549,109782491046,109805390664,109937514797,109945339254,109989420291,109989716099,110001981898,110017507717,110022715851,11002597834,110050641597,110067128478,110096363663,110096398267,110124559945,110134286832,110135522136,11017582539,110188954168,110201810404,110251750777,110278215368,110290648619,110325275488,110349742730,110358041347,110363350050,110368160476,110375962616,110385032388,110401063354,110414461802,110420421060,110450167590,110460971011,110484630787,110507276427,110516249987,110533426554,110547944627,110588873285,110626759851,110648418566,110672390151,110686464626,110689971148,11070113987,110714384770,110723626020,110762143855,11078213516,110791063562,110809452256,110856033988,110863869700,110882426834,110886013214,110913273946,110913322034,110946776168,110963532384,110976194484,110985664529,110998645999,111026632011,111039514233,111046616244,111090622127,111119171953,111123846182,111139425758,111143634524,111145028919,111158009514,111191840866,111204034917,111226475829,111247696100,111257737726,111336384977,111356380086,111366461070,111368265387,111431441219,111473299034,111486260235,111500102872,11152684484,111550110845,111619928659,111639522153,111654016067,111664839644,1116837572,111702689519,111716433653,111758416363,111761910905,111820959167,111848964027,111852983335,111876392392,111895896061,111897417673,111903604706,111987750215,112032714717,112041300860,112045160978,112079313428,112102048161,112122835348,112123988953,11213126449,112138523327,11215948770,112161846446,112164951042,112179281438,112198668466,112263876795,112265563958,112321381067,112334455049,112335674095,112352515999,112354202799,112358117692,112386616299,112423745487,112427999316,112429538898,112442335385,112442703493,112442981803,112452419107,112472199757,112485246531,112506853998,112558331584,112567852606,112571201177,112579283246,112602149348,112603843989,112607563661,112611575849,11261740684,112633681833,112654628780,112656743761,112669694704,112673841135,112690317546,112691084046,112718052625,112780390067,112814189870,112814235616,11282778609,112833962286,11284808172,112851658921,112852666874,112860025408,112897475914,112898211011,112902838312,112913581390,112933402879,112937838093,112940381626,112968199327,112969013552,112976553267,112984268821,11298746454,112990256771,112992061496,112992313456,113007320622,113052181097,113054793132,113073862567,113079032245,113091240459,113093586756,113111102768,113186846818,113202590734,113209078652,113231313687,113240367762,113258325094,113277575744,113289899736,113292117848,113311775395,113313477887,113324516889,113325029205,113333280054,113351964867,113353693407,113365529198,113389298609,11340394158,113407815290,113414222656,113417128905,113451373698,113493906028,113499468953,113510243635,113532283332,113538078524,113541507677,113543617574,113571023829918,113577214401,113577797510,113607645782,113629049789,113646434053,113650577272,11365150438,113707256426,113717626019,113729338103,113757861284,113769413332,113783577155,113833011822,113838364500,113839341202,113839720762,113892473523,113915294683,113920885931,113927415658,11397218294,114023400529,114034619461,114074519970,114178013250,114194155289,114204346698,114209196610,114228757890,114243065658,114307147209,11432499261,114339130946,114341092670,114346294271,114418662525,114454832814,114455466309,114465855514,114482526367,114488223792,114489999047,11449543593,114505207440,11450527254,114505715124,114508279657,114528170069,114547457642,114567725067,114585159513,114619664511,114620218200,114623612793,114639154038,114658711599,114665599297,114674118288,114682832211,114707122445,11471637565,114732250686,114762040545,11476412267,11476919726,114861105113,114875913470,114881615633,114883074433,114885370191,114886701103,114904985269,114926344000,114933008570,11493353602,114935171689,114949645933,11498086812,115000619204,115004072319,11500923193,115024651712,115032946448,115054778507,115056944870,115092692816,115106248874,115113161788,115114340118,115119762372,115123318240,115145985769,115155975253,115159124924,115161084044,115167471361,115184904113,115219435429,115225703539,115252780686,115283456550,115308694215,115320582468,11532179978,115336524973,115351814623,115358890516,115379884712,115408002644,115429251032,115447741037,115447928762,115456001858,115459124299,115513368125,115518388587,115533195792,115535121318,11553938637,115582969955,115602606970,115622250745,115635965030,115636057800,115659020169,115672806514,115691890947,115692582212,115742426927,115753660008,115775177595,115776421841,115787254454,115808076504,115832845659,115848097551,115874297773,115878673306,115895892516,115897014451,11590945099,115929375674,115936553561,115947592793,115966527850,115987169800,115995514147,116044762014,11604733950,116070051527,116080581547,116087605820,116091270090,116093706519,116105555215,116142165587,116146973249,11615311447,116189841701,116190673462,116200405829,116213284963,116215770104,11622851876,116243556433,116257859344,116276232173,116292097628,116305146770,116307935103,116334850570,116347620463,116373813376,116379968573,11640096627,116427191099,116452550639,116505487568,116532006653,116533805269,116539694161,11655436938,116620125543,116620796706,116622381293,116623942296,116628461442,116637237019,116646209501,11664811283,116649450268,116700329566,116703958839,116724526976,116750960231,116752026029,116754592896,116765586104,11676778731,116771605954,116773213551,116776506635,116795295481,116809246110,116818446448,116824089114,116834646599,116840748828,116874176327,116886871836,116894883342,116934671268,116940305470,116964157561,116983752535,116987410178,116990371173,117000869485,117022816782,117026020554";
        String[] strings = ids.split(",");
        LongAdder longAdder = new LongAdder();
        List<FbAccount> list = Lists.newArrayList();
        List<String> stringList = Arrays.asList(strings);
        List<List<String>> partition = Lists.partition(stringList, 100);
        partition.forEach(o->{
            String str = String.join(" ", o);
            InformationSearchPageParamVO vo = new InformationSearchPageParamVO();
            vo.setKeyword(str);
            vo.setPageNo(0);
            vo.setPageSize(10000);
            ThirdPartySearchPageResponse<FbAccount> response = queryFbInfoPage2(vo);
            if(response.getTotal() > 0){
                List<FbAccount> fbAccountList = (List<FbAccount>) response.getList();
                list.addAll(fbAccountList);
                fbAccountList.forEach(fb->{
                    if(!StringUtils.isBlank(fb.getPhone())){
                        longAdder.increment();
                    }
                });
            }
        });

        String fileName = new StringBuilder("C:\\Users\\ligua\\Desktop\\test").append("/").append("facebook").append(".xlsx").toString();
        File file = new File(fileName);
        if(file.exists()){
            file.delete();
        }
        try (ExcelWriter excelWriter = EasyExcel.write(fileName, FbAccount.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
            excelWriter.write(list, writeSheet);
        }
        System.out.println("账号命中率： " + (double)(Math.round(list.size()*100 / strings.length)/100.0));
        System.out.println("手机号命中率： " + (double)(Math.round(longAdder.longValue()*100 / strings.length)/100.0));
    }

    public ThirdPartySearchPageResponse<FbAccount> queryFbInfoPage2(InformationSearchPageParamVO paramVO) {
        List<FbAccount> list = Lists.newArrayList();
        long total=0, totalPage=1;

        if(paramVO == null){
            return ThirdPartySearchPageResponse.builderEmpty();
        }

        // 创建查询对象
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        if(StringUtils.isNotBlank(paramVO.getKeyword())){
            //builder.withQuery(new TermQueryBuilder( "uid",paramVO.getKeyword()));
            builder.withQuery(new MatchQueryBuilder("uid", paramVO.getKeyword()).operator(Operator.OR));

        }
        builder.withPageable(PageRequest.of(paramVO.getPageNo(), paramVO.getPageSize()));
        NativeSearchQuery query = builder.build();

        //指定查询索引
        IndexCoordinates indexCoordinates = IndexCoordinates.of("socialmedia-facebook");

        // 调用查询接口
        SearchHits<FbAccount> searchHits = null;
        try {
            searchHits = flElasticsearchRestTemplate.search(query, FbAccount.class, indexCoordinates);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(searchHits != null){
            // 记录总数
            total = searchHits.getTotalHits();


            List<SearchHit<FbAccount>> hits = searchHits.getSearchHits();
            if(!CollectionUtils.isEmpty(hits)){
                list  = hits.stream().map(SearchHit::getContent).collect(Collectors.toList());
            }
            return ThirdPartySearchPageResponse.builder(list, total, totalPage, paramVO.getPageNo());
        }
        return ThirdPartySearchPageResponse.builderEmpty();
    }
}
